<template>
  <div class="technology">
    <section>
      <div v-for="(item, i) in rowData" :key="i" class="a-row" v-if="i !== 2">
        <div class="label" v-if="i !== 1">
          {{item.label + (i > 2 ? i : '')}}
        </div>
        <div class="label" v-if="i === 1">
          <el-select v-model="item.sq">
            <el-option v-for="ite in item.label" :key="ite.sq" :label="ite.label" :value="ite.sq"></el-option>
          </el-select>
        </div>
        <div class="row-input">
          <input v-model="item.value" :placeholder="item.placeholder" class="input-item" />
          <el-color-picker v-model="item.color" class="color-picker" :predefine="predefineColors"></el-color-picker>
        </div>
        <svg v-if="i === 0" class="icon-add" style="opacity: 0;cursor: default;" aria-hidden="true" width="24" height="24">
          <use xlink:href="#icon-tianjia"></use>
        </svg>
        <svg v-if="i !== 0" class="icon-add" aria-hidden="true" width="24" height="24" @click="rowBtnClick(1, i)">
          <use xlink:href="#icon-tianjia"></use>
        </svg>
      </div>

      <div v-for="(item, i) in rowDataTwo" :key="i + '2'" class="a-row">
        <div class="label">
          <el-select v-model="item.sq">
            <el-option v-for="ite in item.label" :key="ite.sq" :label="ite.label" :value="ite.sq"></el-option>
          </el-select>
        </div>
        <div class="row-input">
          <input v-model="item.value" :placeholder="item.placeholder" class="input-item" />
          <el-color-picker v-model="item.color" class="color-picker" :predefine="predefineColors"></el-color-picker>
        </div>
        <svg class="icon-add" aria-hidden="true" width="24" height="24" @click="rowBtnClick(-1, i,'Two')">
          <use xlink:href="#icon-jianqu"></use>
        </svg>
      </div>

      <div v-for="(item, i) in rowData" :key="i" v-if="i === 2" style="position:relative;">
        <div class="a-row">
          <div class="label" v-if="i !== 1">
            {{item.label + (i > 2 ? i : '')}}
          </div>
          <div class="label" v-if="i === 1">
            <el-select v-model="item.sq">
              <el-option v-for="ite in item.label" :key="ite.sq" :label="ite.label" :value="ite.sq"></el-option>
            </el-select>
          </div>
          <div class="row-input">
            <input v-model="item.value" :placeholder="item.placeholder" class="input-item" />
            <el-color-picker v-model="item.color" class="color-picker" :predefine="predefineColors"></el-color-picker>
          </div>
          <svg v-if="i === 0" class="icon-add" style="opacity: 0;cursor: default;" aria-hidden="true" width="24" height="24">
            <use xlink:href="#icon-tianjia"></use>
          </svg>
          <svg v-if="i !== 0" class="icon-add" aria-hidden="true" width="24" height="24" @click="rowBtnClick(1, i)">
            <use xlink:href="#icon-tianjia"></use>
          </svg>
        </div>
        <tool-query tabs="IPC" :tool-sq.sync="item.value"  :key="i + '2'" style="position:absolute;right:44px;top:45px;"></tool-query>
      </div>

      <div v-for="(item, i) in rowDataThree" :key="i + '3'" style="position:relative;">
        <div class="a-row">
          <div class="label">
            {{item.label}}
          </div>
          <div class="row-input">
            <input v-model="item.value" :placeholder="item.placeholder" class="input-item" />
            <el-color-picker v-model="item.color" class="color-picker" :predefine="predefineColors"></el-color-picker>
          </div>
          <svg class="icon-add" aria-hidden="true" width="24" height="24" @click="rowBtnClick(-1, i,'Three')">
            <use xlink:href="#icon-jianqu"></use>
          </svg>
        </div>
        <!-- <tool-query tabs="IPC" :tool-sq.sync="item.value" :key="i + '3'" style="position:absolute;right:44px;top:45px;"></tool-query> -->
      </div>

      <div class="search-footer">
        <div class="data-source btn" @click="openDataSource">
          <i class="iconfont icon-xuanzeshujuyuan_new"></i>
          <span>选择数据源</span>
        </div>
        <el-button @click="search" class="searchBtn btn">检索</el-button>
      </div>
    </section>
    <!-- 数据来源 -->
    <!-- <DataSource ref="dataSource"/> -->
  </div>
</template>

<script type="text/ecmascript-6">
// import DataSource from '../DataSource'
import ToolQuery from './ToolQuery';
import Ecolors from "@/utils/chartsColors.js";
export default {
  name: "technology",
  components: {
    // DataSource,
    ToolQuery
  },
  data() {
    return {
      predefineColors:Ecolors.color.slice(0,10),
      toolSq:"",
      dataResourceShow: false,
      rowData: [
        {
          label: "专利号",
          sq: "RDI/",
          color: Ecolors.color[0],
          value: "",
          placeholder:"请输入专利号"
        },
        {
          label: [
            {
              //关键字
              sq: "A/",
              label: "标题，摘要，权利要求"
            },
            {
              sq: "TTL/|||ABST/",
              label: "标题，摘要"
            },
            {
              sq: "TTL/",
              label: "标题"
            },
            {
              sq: "ABST/",
              label: "摘要"
            },
            {
              sq: "ACLM/",
              label: "权利要求"
            },
            {
              sq: "SPEC/",
              label: "说明书"
            },
            {
              sq: "B/",
              label: "技术特征"
            }
          ],
          sq: "B/",
          color: Ecolors.color[0],
          value: "",
          placeholder:"选填，输入关键词"
        },
        {
          label: "IPC(国际分类)",
          sq: "ICL/",
          color: Ecolors.color[0],
          value: "",
          placeholder:"选填，输入分类号"
        }
      ],
      rowDataTwo: [],
      rowDataThree: []
    };
  },
  methods: {
    /*
    * params: 1:加， -1减, 索引
    * return: 无
    * function: 行增加或减
    * */
    rowBtnClick(type, i, n) {
      if (type === 1 && i === 1) {
        this.rowDataTwo.push({
          label: [
            {
              //关键字
              sq: "A/",
              label: "标题，摘要，权利要求"
            },
            {
              sq: "TTL/|||ABST/",
              label: "标题，摘要"
            },
            {
              sq: "TTL/",
              label: "标题"
            },
            {
              sq: "ABST/",
              label: "摘要"
            },
            {
              sq: "ACLM/",
              label: "权利要求"
            },
            {
              sq: "SPEC/",
              label: "说明书"
            },
            {
              sq: "B/",
              label: "技术特征"
            }
          ],
          sq: "B/",
          color: Ecolors.color[0],
          value: "",
          placeholder:"选填，输入关键词"
        });
      } else if (type === 1 && i === 2) {
        this.rowDataThree.push({
          label: "IPC(国际分类)",
          sq: "ICL/",
          color: Ecolors.color[0],
          value: "",
          placeholder:"选填，输入分类号"
        });
      } else if (type === -1) {
        this["rowData" + n].splice(i, 1);
      }
    },
    /*
    * params: 无
    * return: 检索式
    * function: 获取检索式
    * */
    getSq() {
      let arr = [];
      let highlightKeyWords = [];
      this.rowData.forEach(item => {
        let a = [];
        let aSq = item.sq.split("|||") || [];
        if (item.value) {
          aSq.forEach(x => {
            a.push(x + item.value);
          });
          arr.push("(" + a.join(" AND ") + ")");
          highlightKeyWords.push({
            words: item.value.trim(),
            color: item.color
          });
        }
      });

      this.rowDataTwo.forEach(item => {
        let a = [];
        let aSq = item.sq.split("|||") || [];
        if (item.value) {
          aSq.forEach(x => {
            a.push(x + item.value);
          });
          arr.push("(" + a.join(" AND ") + ")");
          highlightKeyWords.push({
            words: item.value.trim(),
            color: item.color
          });
        }
      });

      this.rowDataThree.forEach(item => {
        let a = [];
        let aSq = item.sq.split("|||") || [];
        if (item.value) {
          aSq.forEach(x => {
            a.push(x + item.value);
          });
          arr.push("(" + a.join(" AND ") + ")");
          highlightKeyWords.push({
            words: item.value.trim(),
            color: item.color
          });
        }
      });

      this.$store.commit("setHighlightKeyWords", {
        modes: highlightKeyWords,
        open: true
      })

      return arr.join(" AND ");
    },
    /*
    * params: 无
    * return: 无
    * function: 检索
    * */
    search() {
      // let key = `sq${new Date().getTime()}`;
      let sq = this.getSq();
      if (!sq) {
        this.$message({
          type: "error",
          duration: 1500,
          message: '请输入内容'
        });
        return false;
      }

      this.$store.commit("setSearchWord", sq);

      // 暂时先注释
      // this.$nextTick(() => {
      //   this.$router.push({
      //     name: 'PLSearchResult'
      //   })
      // });
      // sessionStorage.setItem(key, sq); //用于检索
      // window.sessionStorage.setItem("userSearch", "true");
      // window.open("loggedIn.html#/SearchResult?key=" + key, "_blank");
    },
    // 选择数据源
    openDataSource() {
      this.$emit("openDataSource");
      // this.$refs.dataSource.init()
    },
  }
};
</script>

<style lang="scss">
.technology {
  position: relative;
  width: 100%;
  padding: 20px 10px 80px 20px !important;
  border: 2px solid $color141 !important;
  border-radius: 8px;
  margin-top: 6px !important;
  box-sizing: border-box;
  background-color: #fff;
  * {
    box-sizing: border-box;
  }
  .search-title {
    height: 38px;
    line-height: 38px;
    font-size: 18px;
    color: $mainColor;
    font-weight: 700;
    margin-bottom: 20px;
  }

  .a-row {
    display: flex;
    align-items: center;
    height: 40px;
    margin-bottom: 20px;
    .label {
      flex: none;
      height: 100%;
      line-height: 40px;
      width: 180px;
      padding-left: 20px;
      margin-right: 10px;
      border-radius: 3px;
      background: #eef3fa;
    }
    .el-input__inner {
      font-size: 12px;
      padding-left: 0;
      background-color: #eef3fa;
      border: 0;
    }
    .row-input {
      flex: auto;
      position: relative;
      margin: 0;
      padding-right: 20px;

      .input-item {
        width: 100%;
        height: 40px;
        line-height: 40px;
        padding: 0 40px 0 20px;
        border: none;
        border-radius: 3px;
        outline: none;
        background: #eef3fa;
      }
      .color-picker {
        position: absolute;
        right: 40px;
        top: 13px;
        width: 15px;
        height: 15px;

        .el-color-picker__trigger {
          width: 100%;
          height: 100%;
          padding: 0;
          border: none;
        }
        .el-color-picker__color {
          border: none;
        }
        .el-color-picker__icon {
          display: none;
        }
      }
    }
    .icon-add {
      cursor: pointer;
    }
  }

  .search-footer {
    position: relative;
    height: 100px;
    .data-source {
      display: inline-flex;
      align-items: center;
      justify-content: flex-start;
      height: 30px;
      color: $mainColor;
      i {
        font-size: 20px;
        cursor: pointer;
      }
      span {
        font-size: 14px;
        cursor: pointer;
      }
    }

    .searchBtn {
      position: absolute;
      right: 40px;
      bottom: 10px;
      font-size: 12px;
      padding: 8px 37px;
      background-color: $mainColor;
      color: #fff;
      font-weight: bold;
      border-radius: 15px;
      border: 1px solid $mainColor;
    }
  }
}
</style>
